Fix potential double-close issue in sharedEncryption #1446
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Potential Double-Close Issue in Session Cache
Problem
The
sharedEncryption.Remove()method had no idempotency protection, creating a potential double-close vulnerability that could cause panics or undefined behavior in production environments.Vulnerable Code Path:
Risk Scenarios:
Remove()callsRemove()multiple timesEncryption.Close()implementations may panic on repeated callsSolution
Implemented bulletproof idempotency using Go's
sync.Onceprimitive to guarantee the underlyingEncryption.Close()is called exactly once, regardless of how many timesRemove()is invoked.Protected Implementation:
Performance Impact
Virtually Zero Performance Cost:
int32+ pointersync.Once Performance Characteristics:
Safety & Reliability Benefits
Idempotency Guarantees
Production Robustness
Error Recovery
Architecture Validation
No Pooling/Reuse Conflicts:
Encryption.Close()is documented as final cleanup ("frees up any resources")sharedEncryptioninstances are created fresh per session (no pooling)Remove()only called during cache eviction (disposal context)sync.Once Design Perfect Fit:
Comprehensive Testing
Added thorough test coverage with realistic scenarios:
Test Coverage Matrix
Remove()calls in sequenceClose()called exactly onceRemove()simultaneouslyClose()Close()andRemove()callsRepresentative Test Case
Risk Assessment
Extremely Low Risk Change:
Backwards Compatibility:
Monitoring & Observability
No Special Monitoring Required:
Positive Indicators:
Production Benefits
High-Traffic Environments
Serverless/Lambda Environments
Long-Running Services
Summary
This fix implements a standard Go reliability pattern (
sync.Once) to eliminate a potential double-close vulnerability while maintaining perfect backwards compatibility and adding virtually zero performance overhead. The change transforms a potential production reliability issue into a safely handled no-op scenario.Key Outcomes:
🤖 Generated with Claude Code